package com.kws.payment.business;

import com.kws.payment.dao.CreateWeixinPayOrderRq;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.payments.h5.H5Service;
import com.wechat.pay.java.service.payments.h5.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * <b>WeixinH5PayBusiness</b>
 *
 * @description: WeixinH5PayBusiness <br>
 * @date: 2024/11/13 15:10 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */
@Component
public class WeixinH5PayBusiness {
    private static final Logger logger = LoggerFactory.getLogger(WeixinH5PayBusiness.class);

    /** 商户号 */
    @Value("${weixinPay.merchantId}")
    public String merchantId;
    /** 商户API私钥路径 */
    @Value("${weixinPay.privateKey_path}")
    public String privateKeyPath;
    /** 商户证书序列号 */
    @Value("${weixinPay.merchant_serial_number}")
    public String merchantSerialNumber;
    /** 商户APIV3密钥 */
    @Value("${weixinPay.api_key}")
    public String apiV3key;

    @Value("${weixinPay.app_id}")
    public String appId;

    @Value("${weixinPay.mchid}")
    public String mchid;

    @Value("${weixinPay.notify_url}")
    public String notifyUrl;

    /**
     * 微信H5页面支付预下单
     */
    public String createWeixinH5PayOrder(CreateWeixinPayOrderRq rq){
        Config config =
                new RSAAutoCertificateConfig.Builder()
                        .merchantId(merchantId)
                        .privateKeyFromPath(privateKeyPath)
                        .merchantSerialNumber(merchantSerialNumber)
                        .apiV3Key(apiV3key)
                        .build();
        H5Service service = new H5Service.Builder().config(config).build();
        PrepayRequest request = new PrepayRequest();
        Amount amount = new Amount();
        //订单金额 单位分
        amount.setTotal(rq.getAmount());
        request.setAppid(appId);
        request.setMchid(mchid);
        request.setAmount(amount);
        //商品描述
        request.setDescription(rq.getDescription());
        //订单号
        request.setOutTradeNo(rq.getOrderNo());
        //支付完成回调地址
        request.setNotifyUrl(notifyUrl);
        //【场景信息】场景信息
        SceneInfo sceneInfo = new SceneInfo();
        //终端ip地址
        sceneInfo.setPayerClientIp(rq.getPayerClientIp());
        H5Info h5Info = new H5Info();
        //场景类型
        h5Info.setType(rq.getType());
        sceneInfo.setH5Info(h5Info);
        request.setSceneInfo(sceneInfo);
        PrepayResponse response = service.prepay(request);
        logger.info("微信H5支付下单返回结果:"+response.getH5Url());
        return response.getH5Url();
    }

    /**
     * 微信h5支付关闭订单
     */
    public void weixinH5PayCloseOrder(String orderNo){
        Config config =
                new RSAAutoCertificateConfig.Builder()
                        .merchantId(merchantId)
                        .privateKeyFromPath(privateKeyPath)
                        .merchantSerialNumber(merchantSerialNumber)
                        .apiV3Key(apiV3key)
                        .build();
        H5Service service = new H5Service.Builder().config(config).build();
        CloseOrderRequest request = new CloseOrderRequest();
        request.setMchid(mchid);
        request.setOutTradeNo(orderNo);
        service.closeOrder(request);
    }
}
